var HxBasicGoodsInfoDlg = {
    data: {
        id:"",
        goodsName: "",
        goodsNumber: "",
    }
};
var BasicPutgoodsInfoDlg = {
    data: {
        id:"",
        dictId:"",
        putgoodsCustomerId: "",
        putgoodsOperateName: "",
        putgoodsName: "",
    }
};
var DictInfoDlg = {
    data: {
        dictId: "",
        name: ""
    }
};
layui.use(['table', 'admin', 'ax', 'laydate', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem:'#startTime'
        ,value:new Date()
    });
    laydate.render({
        elem:'#endTime'
        ,value:new Date()
    });

    /**
     * 库存表管理
     */
    var HxStorageStock = {
        tableId: "hxStorageStockTable"
    };

    var sl = {
        baseNum:"",
        kcNum:"",
        cgNum:"",
        pfNum:"",
        psNum:"",
        stockWeight:"",
        stockVolume:"",
        stockMoney:""
    }

    /**
     * 初始化表格的列kc  mx
     */
    HxStorageStock.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'kcUnit', sort: true, title: '库存单位', totalRowText: '合计：'},
            {field: 'kcNum', sort: true, title: '库存数量', totalRowText: sl.kcNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},
            
            {field: 'expired1', sort: true, title: '未到3/1期' , templet: function(d){
                    if (d.expired1) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired2', sort: true, title: '超3/1期' , templet: function(c){
                    if (c.expired2) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired3', sort: true, title: '超2/1期' , templet: function(b){
                    if (b.expired3) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired', sort: true, title: '过期' , templet: function(a){
                    if (a.expired) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            
            {field: 'storageDays', sort: true, title: '库龄'},
            {field: 'batch', sort: true, title: '批次'},
            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'trayNumber', sort: true, title: '托盘'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
            {field: 'expirationDate', sort: true, title: '有效期'},
            {field: 'setUnit', sort: true, title: '发货客户'},

        ]];
    };
    /**
     * 初始化表格的列 cg   mx
     */
    HxStorageStock.initColumn1 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'cgUnit', sort: true, title: '采购单位', totalRowText: '合计：'},
            {field: 'cgNum', sort: true, title: '采购数量', totalRowText: sl.cgNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},
            
            {field: 'expired1', sort: true, title: '未到3/1期' , templet: function(d){
                    if (d.expired1) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired2', sort: true, title: '超3/1期' , templet: function(c){
                    if (c.expired2) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired3', sort: true, title: '超2/1期' , templet: function(b){
                    if (b.expired3) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired', sort: true, title: '过期' , templet: function(a){
                    if (a.expired) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'storageDays', sort: true, title: '库龄'},
            {field: 'batch', sort: true, title: '批次'},
            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'trayNumber', sort: true, title: '托盘'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
            {field: 'expirationDate', sort: true, title: '有效期'},
            {field: 'setUnit', sort: true, title: '发货客户'},

        ]];
    };
    /**
     * 初始化表格的列 pf   mx
     */
    HxStorageStock.initColumn2 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'pfUnit', sort: true, title: '批发单位', totalRowText: '合计：'},
            {field: 'pfNum', sort: true, title: '批发数量', totalRowText: sl.pfNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},
            
            {field: 'expired1', sort: true, title: '未到3/1期' , templet: function(d){
                    if (d.expired1) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired2', sort: true, title: '超3/1期' , templet: function(c){
                    if (c.expired2) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired3', sort: true, title: '超2/1期' , templet: function(b){
                    if (b.expired3) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired', sort: true, title: '过期' , templet: function(a){
                    if (a.expired) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'storageDays', sort: true, title: '库龄'},
            {field: 'batch', sort: true, title: '批次'},
            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'trayNumber', sort: true, title: '托盘'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
            {field: 'expirationDate', sort: true, title: '有效期'},
            {field: 'setUnit', sort: true, title: '发货客户'},

        ]];
    };
    /**
     * 初始化表格的列   ps   mx
     */
    HxStorageStock.initColumn3 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},
            
            {field: 'psUnit', sort: true, title: '配送单位', totalRowText: '合计：'},
            {field: 'psNum', sort: true, title: '配送数量', totalRowText: sl.psNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},
            
            {field: 'expired1', sort: true, title: '未到3/1期' , templet: function(d){
                    if (d.expired1) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired2', sort: true, title: '超3/1期' , templet: function(c){
                    if (c.expired2) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired3', sort: true, title: '超2/1期' , templet: function(b){
                    if (b.expired3) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'expired', sort: true, title: '过期' , templet: function(a){
                    if (a.expired) {
                        return '√';
                    } else {
                        return '';
                    }
                }},
            {field: 'storageDays', sort: true, title: '库龄'},
            {field: 'batch', sort: true, title: '批次'},
            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'trayNumber', sort: true, title: '托盘'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
            {field: 'expirationDate', sort: true, title: '有效期'},
            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };

    HxStorageStock.kais = function () {
        $.ajax({
            url:Feng.ctxPath + '/hxStorageStock/list2?stockUnit=kc&queryMode=mx',
            dataType:"json",   //返回格式为json
            async: false,
            type:"post",   //请求方式
            success:function(data){
                sl.kcNum = data.data.kcNum
                sl.baseNum = data.data.baseNum
                sl.stockWeight = data.data.stockWeight
                sl.stockVolume = data.data.stockVolume
                sl.stockMoney = data.data.stockMoney
            }
        });

    };

    HxStorageStock.kais();
    
    // 渲染表格
    tableResult = table.render({
        elem: '#' + HxStorageStock.tableId,
        url: Feng.ctxPath + '/hxStorageStock/list',
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: HxStorageStock.initColumn()
    });

    /**
     * 点击查询按钮
     */
    HxStorageStock.search = function () {
        var queryData = {};
        queryData['stockState'] = $("#stockState").val();
        queryData['setUnit'] = $("#setUnit").val();
        queryData['goodsBigtypeId'] = $("#goodsBigtypeId").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['areaId'] = $("#areaId").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        queryData['positionType'] = $("#positionType").val();
        queryData['trayNumber'] = $("#trayNumber").val();
        queryData['condition'] = $("#condition").val();
        
        var stockUnit=$("#stockUnit").val();
        var queryMode=$("#queryMode").val();
        if (stockUnit=="kc"&&queryMode=="mx"){
            $.ajax({
                url:Feng.ctxPath + '/hxStorageStock/list2?stockUnit=kc&queryMode=mx',
                dataType:"json",   //返回格式为json
            	data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type:"post",   //请求方式
                success:function(data){
                    sl.kcNum = data.data.kcNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=kc&queryMode=mx',
            	where: queryData,
                page: true,
                limit: 150,
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true ,//开启合计行
                cols: HxStorageStock.initColumn()
            });
        }

        if (stockUnit=="cg"&&queryMode=="mx"){
            $.ajax({
                url:Feng.ctxPath + '/hxStorageStock/list2?stockUnit=cg&queryMode=mx',
                dataType:"json",   //返回格式为json
            	data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type:"post",   //请求方式
                success:function(data){
                    sl.cgNum = data.data.cgNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=cg&queryMode=mx',
            	where: queryData,
                page: true,
                limit: 150,
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true ,//开启合计行
                cols: HxStorageStock.initColumn1()
            });
        }
        if (stockUnit=="pf"&&queryMode=="mx"){
            $.ajax({
                url:Feng.ctxPath + '/hxStorageStock/list2?stockUnit=pf&queryMode=mx',
                dataType:"json",   //返回格式为json
            	data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type:"post",   //请求方式
                success:function(data){
                    sl.pfNum = data.data.pfNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=pf&queryMode=mx',
            	where: queryData,
                page: true,
                limit: 150,
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true ,//开启合计行
                cols: HxStorageStock.initColumn2()
            });
        }
        if (stockUnit=="ps"&&queryMode=="mx"){
            $.ajax({
                url:Feng.ctxPath + '/hxStorageStock/list2?stockUnit=ps&queryMode=mx',
                dataType:"json",   //返回格式为json
            	data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type:"post",   //请求方式
                success:function(data){
                    sl.psNum = data.data.psNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=ps&queryMode=mx',
            	where: queryData,
                page: true,
                limit: 150,
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true ,//开启合计行
                cols: HxStorageStock.initColumn3()
            });
        }
    };

    //查询库区
    form.on('select(warehouseId)',function(){
        $("#areaId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicArea/Querylist',
            dataType:"json",   //返回格式为json
            async: false,
            data:{"areaWarehouseId":$("#warehouseId").val()},    //参数值
            type:"post",   //请求方式
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>选择库区</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.areaName+"</option>";
                    });
                    $("#areaId").html(str);
                }
            }
        });
        form.render('select');
    });
    /**
     * 导出excel按钮
     */
    HxStorageStock.exportExcel = function () {
        var checkRows = table.checkStatus(HxStorageStock.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
	        queryData['stockState'] = $("#stockState").val();
	        queryData['setUnit'] = $("#setUnit").val();
	        queryData['goodsBigtypeId'] = $("#goodsBigtypeId").val();
	        queryData['goodsName'] = $("#goodsName").val();
	        queryData['customerId'] = $("#customerId").val();
	        queryData['warehouseId'] = $("#warehouseId").val();
	        queryData['areaId'] = $("#areaId").val();
	        queryData['positionNumber'] = $("#positionNumber").val();
	        queryData['positionType'] = $("#positionType").val();
	        queryData['trayNumber'] = $("#trayNumber").val();
	        queryData['condition'] = $("#condition").val();
        
	        var stockUnit=$("#stockUnit").val();
	        var queryMode=$("#queryMode").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/hxStorageStock/listAll?stockUnit="+stockUnit+"&queryMode="+queryMode,
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };
    // 导出excel
    $('#btnExp').click(function () {
        HxStorageStock.exportExcel();
    });
    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    HxStorageStock.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/hxStorageStock/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    HxStorageStock.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxStorageStock/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(HxStorageStock.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    //点击选择商品档案
    $('#choiceGoods').click(function () {
        layer.open({
            type: 2,
            title: '商品档案',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/hxBasicGoods/toChoiceGoods',
            end: function () {
                $("#goodsName").val(HxBasicGoodsInfoDlg.data.goodsName);//商品名称
            }
        });
    });

    //点击选择商品类别
    $('#goodsType').click(function () {
        var formName = encodeURIComponent("parent.DictInfoDlg.data.name");
        var formId = encodeURIComponent("parent.DictInfoDlg.data.dictId");
        var treeUrl = encodeURIComponent("/hxBasicGoodsCode/treeo");

        layer.open({
            type: 2,
            title: '所属分类',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#goodsBigtypeId").val(DictInfoDlg.data.dictId);
                $("#goodsBigtype").val(getCaption(DictInfoDlg.data.name));
            }
        });
    });

    //选择发货客户
    $('#choiceSetUnit').click(function () {
        layer.open({
            type: 2,
            title: '发货客户',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/basicPutgoods/tochoiceBasicPutgoods_hx',
            end: function () {
                $("#setUnit").val(BasicPutgoodsInfoDlg.data.putgoodsName);
            }
        });
    });
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxStorageStock.search();
    });

    // 重置按钮点击事件
    $('#reset').click(function () {
        $("#goodsName").val("")
        $("#condition").val("")
        $("#goodsBigtype").val("")
        $("#setUnit").val("")
        $("#positionNumber").val("")
        $("#trayNumber").val("")
    });


    // 工具条点击事件
    table.on('tool(' + HxStorageStock.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxStorageStock.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxStorageStock.onDeleteItem(data);
        }
    });
});
